行継続文字エラーの後に予期しない文字が表示されます (I am getting an unexpected character after line continuation character error)


問題の説明

行継続文字エラーの後に予期しない文字が表示されます (I am getting an unexpected character after line continuation character error)

I am getting an unexpected character after line continuation character error in this line

 print (\t,"Order total $",format(total, "10.2"),\n\t,"Discount    $",format(disc,"10.2"),\n\t,"Amount Due $",format (due, "10.2"),sep="")

could someone tell me what that means and how to fix it?  thanks

def finddiscount(quantity):
        if quantity >= 1 and quantity <= 9:
            discount = 0
        elif quantity >= 10 and quantity <= 19:
            discount = .2
        elif quantity >= 20 and quantity <= 49:
            discount = .30
        elif quantity >= 50 and quantity <= 99:
            discount = .40
        elif quantity >= 100:
            discount = .50
    def calctotal(quantity, price):
        disc = (price*quantity)*finddiscount(quantity)
        total = (price*quantity)
        due = (price*quantity)‑(price*quantity)*dicount
        print (\t,"Order total $",format(total, "10.2"),\n\t,"Discount    $",format(disc,"10.2"),\n\t,"Amount Due $",format (due, "10.2"),sep="")
    def main():
        quantity = int(input("How many packages where purchased?"))
        price = float(input("How much is each item?"))
        calctotal(quantity, price)
    main()

リファレンスソリューション

方法 1:

You've forgot to use quotes around many items on this line:

print ("\t","Order total $",format(total, "10.2"),"\n\t","Discount    $",format(disc,"10.2"),"\n\t","Amount Due $",format (due, "10.2"),sep="")
        ^                                           ^                                          ^

And another way to format is to use str.format like this:

print ("\tOrder total $ {:10.2}\n\tDiscount    ${:10.2}\n\tAmount Due ${:10.2}".format(total, disc, due))

方法 2:

Ashwini's answer explains why your code gives the error it does.

But there's a much simpler way to do this. Instead of printing a bunch of strings separated by commas like this, just put the strings together:

print("\tOrder total $", format(total, "10.2"),
      "\n\tDiscount    $", format(disc, "10.2"),
      "\n\tAmount Due $", format(due, "10.2"), sep="")

(I also fixed everything to fit on an 80‑column screen, which is a standard for good reasons—for one thing, it's actually readable on things like StackOverflow; for another, it makes it much more obvious that your code doesn't actually line up the way you wanted it to…)

In this case, it would probably be even better to use three separate print calls. Then you don't need those \n characters in the first place:

print("\tOrder total $", format(total, "10.2"), sep="")
print("\tDiscount    $", format(disc, "10.2"), sep="")
print("\tAmount Due $", format(due, "10.2"), sep="")

Meanwhile, since you're already using the format function, you should have no trouble learning about the format method, which makes things even simpler. Again, you can use three separate statements—but in this case, maybe a multi‑line (triple‑quoted) string would be easier to read:

print("""\tOrder total ${:10.2}
\tDiscount    ${:10.2}
\tAmount Due ${:10.2}""".format(total, disc, due))

See the tutorial section on Fancier Output Formatting for more details on all of this.

(by user3285386Ashwini Chaudharyabarnert)

リファレンスドキュメント

  1. I am getting an unexpected character after line continuation character error (CC BY‑SA 3.0/4.0)

#printing #Python #character






関連する質問

Winspoolを使用してプリンタステータスにアクセスする (Access Printer Status using Winspool)

jQuery プラグインを使用すると、ページ全体が IE で印刷される (Entire page is printing in IE when using jQuery plugin)

行継続文字エラーの後に予期しない文字が表示されます (I am getting an unexpected character after line continuation character error)

コマンドラインから改行を使用して出力を印刷する方法 (How to print output with linebreakers from command line)

マーカーと markerClusters を含む Google マップを印刷する (Print Googlemap with markers and markerClusters)

erlang でセルのグリッドを印刷する (Printing a Grid of cells in erlang)

Bluetooth印刷のフォントサイズを設定するには? (how to set font size for bluetooth printing?)

なぜ最後の入力だけがストレージなのですか? (why just the last input is storage?)

c# でビットマップとテキストを含むラベルを印刷および印刷プレビューする (Print & Print Preview a Bitmap plus a Label with Text in it in c#)

Python または Java を使用してプリンターへのフル アクセスを取得する (Get full access to printer using Python or Java)

ベクター全体を印刷するにはどうすればよいですか? (How do I print an entire vector?)

Python - 関数で出力を出力しない (Python - Not printing output in a function)







コメント